# -*-encoding: utf-8 -*-

from utils.logger.LLogger import logger
import scipy.sparse, time

class Termid_List_Sparse_lil:

    def __init__(self, mtil, mtsize):
        self.mTermidList = mtil
        self.mTermSize = mtsize

    def calculateMatrix(self):
        self.coOccurrencyMatrix = scipy.sparse.lil_matrix((self.mTermSize, self.mTermSize))
        self.distanceMatrix = scipy.sparse.lil_matrix((self.mTermSize, self.mTermSize))
        self.occurrencyMatrix = scipy.sparse.lil_matrix((self.mTermSize, 1))
        mTermidListFile = open(self.mTermidList)
        cnt = 1
        while 1:
            lines = mTermidListFile.readlines(10000)
            if not lines:
                break
            lineCount = len(lines)
            sTime = time.time()
            for line in lines:
                #line = line.strip()
                line = line[0: line.find("-1")].strip()
                #print line
                self.calculateAtomMatrix(line)
                #print "[%d][%s]" % (cnt, line)
                logger.info("[%d][%s]" % (cnt, line))
                cnt += 1
            eTime = time.time()
            print "[Count]%s[Time]%s" % (str(lineCount), str(eTime - sTime))
            logger.debug("[Count]%s[Time]%s" % (str(lineCount), str(eTime - sTime)))
        mTermidListFile.close()
        logger.info("%s%s" % ("CO-Occurrency Matrix:\n", self.coOccurrencyMatrix))
        logger.info("%s%s" % ("Distance Matrix:\n", self.distanceMatrix))
        logger.info("%s%s" % ("Occurrency Matrix\n", self.occurrencyMatrix))

    def calculateAtomMatrix(self, line):
        lineItems = line.split(" ")
        #logger.debug("[LineItemsSize:]", str(len(lineItems)))
        #logger.info("[LineItemsSize:]", str(len(lineItems)))
        lineItemsSize = len(lineItems)
        for i in range(lineItemsSize):
            source = (int)(lineItems[i])
            #print self.occurrencyMatrix[source, 0]
            self.occurrencyMatrix[source, 0] += 1
            #print self.occurrencyMatrix[source, 0]
            for j in range(i + 1, lineItemsSize):
                target = (int)(lineItems[j])
                self.coOccurrencyMatrix[source, target] += 1
                #print str(source), "-->", str(target)
                distance = j - i
                currentDistance = self.distanceMatrix[source, target]
                if currentDistance == 0 or distance < currentDistance:
                    self.distanceMatrix[source, target] = distance


class Termid_List_Sparse_dok:

    def __init__(self, mtil, mtsize):
        self.mTermidList = mtil
        self.mTermSize = mtsize

    def calculateMatrix(self):
        self.coOccurrencyMatrix = scipy.sparse.dok_matrix((self.mTermSize, self.mTermSize))
        self.distanceMatrix = scipy.sparse.dok_matrix((self.mTermSize, self.mTermSize))
        self.occurrencyMatrix = scipy.sparse.dok_matrix((self.mTermSize, 1))
        mTermidListFile = open(self.mTermidList)
        cnt = 1
        while 1:
            lines = mTermidListFile.readlines(10000)
            if not lines:
                break
            lineCount = len(lines)
            sTime = time.time()
            for line in lines:
                #line = line.strip()
                line = line[0: line.find("-1")].strip()
                #print line
                self.calculateAtomMatrix(line)
                #print "[%d][%s]" % (cnt, line)
                logger.info("[%d][%s]" % (cnt, line))
                cnt += 1
            eTime = time.time()
            print "[Count]%s[Time]%s" % (str(lineCount), str(eTime - sTime))
            logger.debug("[Count]%s[Time]%s" % (str(lineCount), str(eTime - sTime)))
        mTermidListFile.close()
        logger.info("%s%s" % ("CO-Occurrency Matrix:\n", self.coOccurrencyMatrix))
        logger.info("%s%s" % ("Distance Matrix:\n", self.distanceMatrix))
        logger.info("%s%s" % ("Occurrency Matrix\n", self.occurrencyMatrix))

    def calculateAtomMatrix(self, line):
        lineItems = line.split(" ")
        #logger.debug("[LineItemsSize:]", str(len(lineItems)))
        #logger.info("[LineItemsSize:]", str(len(lineItems)))
        lineItemsSize = len(lineItems)
        for i in range(lineItemsSize):
            source = (int)(lineItems[i])
            #print self.occurrencyMatrix[source, 0]
            self.occurrencyMatrix[source, 0] += 1
            #print self.occurrencyMatrix[source, 0]
            for j in range(i + 1, lineItemsSize):
                target = (int)(lineItems[j])
                self.coOccurrencyMatrix[source, target] += 1
                #print str(source), "-->", str(target)
                distance = j - i
                currentDistance = self.distanceMatrix[source, target]
                if currentDistance == 0 or distance < currentDistance:
                    self.distanceMatrix[source, target] = distance
